<template>
  <div class="groupKind">
    <div class="toolBar">
      <el-input
        v-model="searchWord"
        size="small"
        @clear="getGroupTypeList"
        style="width: 30%"
        type="text"
        placeholder="BU/部门名称"
        clearable
      >
        <i slot="prefix" class="el-input__icon el-icon-search"></i>
        <el-button
          @click="getGroupTypeList"
          slot="append"
          icon="el-icon-search"
        ></el-button>
      </el-input>
      <el-button
        @click="showAdd = true"
        style="margin-left: 10px"
        size="small"
        type="primary"
        ><i class="el-icon-plus"></i> 新建群组</el-button
      >
      <el-button @click="deleteKind" size="small" type="danger"
        ><i class="el-icon-delete"></i> 删除选中</el-button
      >
    </div>
    <div class="content">
      <el-table
        border
        stripe
        size="small"
        :highlight-current-row="true"
        :data="tableData"
        style="width: 100%"
        height="490"
        @selection-change="selectDelete"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="kindName" label="群组名称"></el-table-column>
        <el-table-column prop="kindCode" label="所属类型">
          <template slot-scope="scope">
            <span>{{ scope.row.kindCode == "bu" ? "BU" : "部门" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="parentId" label="父级名称">
          <template slot-scope="scope">
            <span>{{
              scope.row.parentName ? scope.row.parentName.kindName : "/"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建日期"></el-table-column>
        <el-table-column align="center" label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button @click="edit(scope.row)" size="mini" type="primary"
              ><i class="el-icon-edit"></i
            ></el-button>
            <el-button
              @click="removeKind(scope.row.id)"
              size="mini"
              type="danger"
              ><i class="el-icon-delete"></i
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          small
          background
          layout="total, prev, pager, next"
          :total="total"
          @current-change="goSize"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog title="新增群组分类" :visible.sync="showAdd">
      <el-form size="small">
        <el-form-item label="群组名称">
          <el-input
            style="width: 70%"
            v-model="add.kindName"
            placeholder="群组名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="所属类型">
          <el-radio-group v-model="add.kindCode">
            <el-radio label="bu">BU</el-radio>
            <el-radio label="dept">部门</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="所属父级">
          <el-select v-model="add.parentId">
            <el-option
              v-for="(item, index) in parentList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="diaBtn">
          <el-button type="primary" @click="addKind">确定</el-button>
          <el-button @click="showAdd = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import * as api from "@/js/workTimeApi"
export default {
  name: "GroupKind",
  data() {
    return {
      searchWord: null,
      tableData: [],
      total: 0,
      deleteIdList: [],
      showAdd: false,
      add: {
        kindCode: "dept",
        parentId: 0,
      },
      isEditBtn: false,
      parentList: [],
      pageNum: 1,
      pageSize: 10,
    };
  },
  methods: {
    //跳页
    goSize(currentPage) {
      this.pageNum = currentPage;
      this.getGroupTypeList();
    },
    //查询
    getGroupTypeList() {
      api
        .getGroupKindList({
          kindName: this.searchWord,
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        })
        .then((res) => {
          if (res.status === "success") {
            this.tableData = res.data.records;
            this.total = res.data.total;
            this.tableData.forEach((element, index) => {
              let obj = res.data.records.find((value) => {
                return element.parentId == value.id;
              });
              // console.log(obj);
              this.tableData[index].parentName = obj;
            });
            let temp = [{ value: 0, label: "设为父级" }];
            res.data.records.forEach((element) => {
              temp.push({ value: element.id, label: element.kindName });
            });
            this.parentList = temp;
            // console.log(temp);
          }
        });
        api.getNormalUserMessage();
      api.getPowerUserMessage();
    },
    //选择删除
    selectDelete(data) {
      let temp = [];
      data.forEach((element) => {
        temp.push(element.id);
      });
      // console.log(temp);
      this.deleteIdList = temp;
    },
    removeKind(id) {
      this.deleteIdList = [];
      this.deleteIdList.push(id);
      this.deleteKind();
    },
    //删除
    deleteKind() {
      this.$confirm("此操作将永久删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          api.deleteGroupKind(this.deleteIdList).then((res) => {
            if (res.status == "success") {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getGroupTypeList();
              this.deleteIdList = [];
            } else {
              this.$message({
                type: "error",
                message: res.message,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //新增分类
    addKind() {
      if(this.add.kindName == null || this.add.kindName.trim() == ''){
        this.$message({
          type: 'warning',
          message: '请输入群组名称后操作'
        })
        return;
      }
      if (this.isEditBtn) {
        api.updateGroupKind(this.add).then((res) => {
          if (res.status == "success") {
            this.$message({
              type: "success",
              message: "修改成功",
            });
          } else {
            this.$message({
              type: "error",
              message: res.message,
            });
          }
          this.isEditBtn = false;
          this.showAdd = false;
        });
      } else {
        api.createGroupKind(this.add).then((res) => {
          if (res.status == "success") {
            this.$message({
              type: "success",
              message: "添加成功",
            });
            this.getGroupTypeList();
          } else {
            this.$message({
              type: "error",
              message: res.message,
            });
          }
          this.showAdd = false;
        });
      }
    },
    //编辑
    edit(data) {
      this.add = data;
      this.showAdd = true;
      this.isEditBtn = true;
    },
  },
  mounted() {
    this.getGroupTypeList();
  },
};
</script>
<style scoped>
.groupKind {
  padding-top: 20px;
}
.toolBar {
  display: flex;
}
.content {
  margin-top: 10px;
}
.diaBtn {
  display: flex;
  justify-content: center;
}
.pagination {
  margin-top: 10px;
  width: 100%;
  text-align: center;
}
</style>